package com.sanpu.kxe.jump.paysdk;

import android.util.Log;

import com.sanpu.kxe.base.KxeApplication;
import com.tencent.mm.sdk.modelpay.PayReq;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * Description：微信支付
 * Created by 薛勇军 on 2018/8/2.
 */

public class WXPayUtil {

    public static void toPay(final Map<String, Object> reqInfo) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (null != reqInfo) {
                        JSONObject json = new JSONObject(reqInfo);
                        if (json != null) {
                            PayReq req = new PayReq();
                            // req.appId = WalletConfig.WEIXIN_APP_ID; // 微信注册用appId
                            req.appId = json.getString("appId");
                            req.partnerId = json.getString("partnerId");
                            req.prepayId = json.getString("prepayId");
                            req.nonceStr = json.getString("nonceStr");
                            req.timeStamp = json.getString("timeStamp");
                            req.packageValue = json.getString("packageValue");
                            req.sign = json.getString("sign");
                            req.extData = "app data"; // optional
                            // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
                            KxeApplication.msgApi.registerApp(req.appId);
                            KxeApplication.msgApi.sendReq(req);
                        } else {
                            Log.d("PAY_GET", "返回错误" + json.optString("retmsg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
